package com.hundsun.fcp.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.hundsun.fcp.R;
import com.hundsun.fcp.base.BaseActivity;
import com.hundsun.fcp.common.AppointFiled.RequestCodeDef;
import com.hundsun.fcp.common.Redirect;
import com.hundsun.fcp.common.TradeRequest;
import com.hundsun.fcp.common.utils.CommonDialog;
import com.hundsun.fcp.common.utils.MD5;
import com.hundsun.fcp.common.utils.StringUtils;
import com.hundsun.fcp.common.utils.ToastUtil;
import com.hundsun.fcp.dialog.LoadingDialog;
import com.hundsun.fcp.manager.FcpUserManager;
import com.hundsun.fcp.manager.SettingManager;
import com.hundsun.fcp.openapi.IOpenApiDataServiceCallback;
import com.hundsun.fcp.openapi.SmsTransCode;
import com.hundsun.fcp.openapi.trade.TradeLoginDataService;
import com.hundsun.fcp.openapi.trade.TradeRegisterDataService;
import com.hundsun.fcp.openapi.trade.bean.RequestLoginBean;
import com.hundsun.fcp.openapi.trade.bean.RequestRegisterBean;
import com.hundsun.fcp.openapi.trade.bean.Result;
import com.hundsun.fcp.widget.EditTextWithClear;
import com.hundsun.fcp.widget.TimerButton;

public class SignupActivity extends BaseActivity implements OnClickListener {
	// 注册的输入的手机号
	private EditTextWithClear mMobile;
	// 收到的验证码
	private EditTextWithClear mSmscode;
	// 短信验证码按钮
	private TimerButton mSmsButton;
	// 注册密码
	private EditTextWithClear mPwd;
	// 是否同意《恒生服务协议》
	private CheckBox mChecked;
	// 《恒生服务协议》跳转文本
	private TextView mHSprotocol;
	// 立即注册按钮
	private Button mSignUpButton;

	private String strMoblie;
	private String strSmscode;
	private String strPwd;
	
	private TradeRegisterDataService mRegisterDataService ;
	
	private RequestLoginBean mLoginBean ;
	private RequestRegisterBean mRegisterBean ;

	@Override
	protected int getContentLayout() {
		return R.layout.fcp_mine_signup;
	}

	@Override
	protected void initHeader() {
		super.initHeader();
		mHeaderMenu.setTitle(R.string.mine_signup);
		mHeaderMenu.setRightText(R.string.mine_btn_login);
		mHeaderMenu.setRightClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Redirect.startLogin(SignupActivity.this);
			}
		});
	}

	private void initServices() {
		mRegisterDataService = new TradeRegisterDataService(this) ;
		mRegisterDataService.setCallback(mRegisterCallback) ;
		mLoginBean = new RequestLoginBean() ;
		mRegisterBean = new RequestRegisterBean() ;
	}
	
	@Override
	public void initView() {
		super.initView();
		initServices() ;
		mMobile = (EditTextWithClear) findViewById(R.id.id_mobile_modify_ipt);
		mSmscode = (EditTextWithClear) findViewById(R.id.id_msgcode_modify_ipt);
		mSmsButton = (TimerButton) findViewById(R.id.id_obtain_msgcode_modify_btn);
		mPwd = (EditTextWithClear) findViewById(R.id.id_pwd_ipt);
		mChecked = (CheckBox) findViewById(R.id.check_signup);
		mHSprotocol = (TextView) findViewById(R.id.tv_check2);
		mSignUpButton = (Button) findViewById(R.id.btn_submit);
		mSmsButton.setOnClickListener(this);
		mSignUpButton.setOnClickListener(this);

		mChecked.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				vfySignBtn();

			}
		});
		mSmscode.addTextChangedListener(mTextWatcher);
		mPwd.addTextChangedListener(mTextWatcher);
		mMobile.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				setMsgCodeBtnEnabled();
				vfySignBtn();
			}
		});

	}

	@Override
	public void onClick(View v) {
		if (v == mSmsButton) {
			// 已经判定输入框不为空,strMobile可直接使用
			strMoblie = mMobile.getText().toString().trim();
			TradeRequest.obtainSms(this, strMoblie, SmsTransCode.TRANSCODE_REGISTER, mSmsButton) ;
		} else if (v == mSignUpButton) {
			// 已经判定三个输入框不为空,strMobile、strSmscode和strPwd可直接使用
			strMoblie = mMobile.getText().toString().trim();
			strSmscode = mSmscode.getText().toString().trim();
			strPwd = mPwd.getText().toString().trim();
			mRegisterBean.mobile = strMoblie ;
			mRegisterBean.password = MD5.getMD5Str(strPwd) ;
			mRegisterBean.checkcode = strSmscode ;
			mRegisterDataService.cancel() ;
			mRegisterDataService.post(mRegisterBean) ;
			LoadingDialog.showLoading(this, "正在注册...", new DialogInterface.OnCancelListener() {
				
				@Override
				public void onCancel(DialogInterface dialog) {
					mRegisterDataService.cancel() ;
				}
			}) ;
		} else if (v == mHSprotocol) {
			// TODO 跳转《恒生服务协议》

		}
	}

	/**
	 * 根据手机号码是否为空来控制 获取验证码 按钮
	 */
	private void setMsgCodeBtnEnabled() {
		strMoblie = mMobile.getText().toString();
		if (strMoblie.length() > 0 && StringUtils.isMobileNO(strMoblie)) {
			mSmsButton.setEnabled(true);
			mSmsButton.setBackgroundResource(R.color.msgcode_bg);
		} else {
			mSmsButton.setEnabled(false);
			mSmsButton.setBackgroundResource(R.color.signup_msgcode_bg);
		}

	}

	/**
	 * 根据是否输入手机号、验证码和密码来控制确认按钮
	 */
	private void vfySignBtn() {
		strMoblie = mMobile.getText().toString().trim();
		strSmscode = mSmscode.getText().toString().trim();
		strPwd = mPwd.getText().toString().trim();

		if (StringUtils.isEmpty(strMoblie) || StringUtils.isEmpty(strSmscode)
				|| StringUtils.isEmpty(strPwd) || !mChecked.isChecked()) {
			mSignUpButton.setEnabled(false);
			mSignUpButton.setBackgroundResource(R.drawable.btn_grey_unable);
		} else {
			mSignUpButton.setEnabled(true);
			mSignUpButton.setBackgroundResource(R.drawable.btn_red_normal);
		}
	}

	private void ToastShow(String errorMsg) {
		LoadingDialog.dismiss() ;
		ToastUtil.showInfoToast(SignupActivity.this, errorMsg, 1) ;
	}

	private TextWatcher mTextWatcher = new TextWatcher() {

		public void onTextChanged(CharSequence s, int start, int before,
				int count) {

		} 

		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {

		}

		public void afterTextChanged(Editable s) {
			vfySignBtn();
		}
	};
	
	
	private IOpenApiDataServiceCallback mRegisterCallback = new IOpenApiDataServiceCallback<Result>() {
		
		@Override
		public void onGetError(int errorCode, String errorMsg, Throwable error) {
			ToastShow(errorMsg);
		}
		
		@Override
		public void onGetData(Result data) {
			if(data.isSuccess()){
				mLoginBean.mobile = strMoblie ;
				mLoginBean.password = MD5.getMD5Str(strPwd) ; 
				TradeRequest.doLogin(SignupActivity.this, mLoginBean, true, true) ;
			}else{ 
				ToastShow(data.info);
			}
		}
	};
	
	 
	@Override
	public void afterLogin() {
		Intent intent = new Intent(SignupActivity.this, MainActivity.class) ;
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) ;
		startActivity(intent) ;
		finish() ;
	}

}
